<?php

/**
 * post actions.
 *
 * @package    workforoptions
 * @subpackage post
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class postActions extends sfJSONActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    $this->forward('default', 'module');
  }
  
  public function executeShow()
  {
  
   $this->post = $post = workpostsPeer::retrieveByPk($this->getRequestParameter('id'));  
   
   $postcommentsc = new Criteria();
   
   if ($this->getUser()->getSignedUserId() == $post->getAuthorId()) {
   
   } else {
   
   $postcommentsc->add(workpostscommentPeer::PRIVATECOMMENT,false);
   
   }
   
   
   $postcommentsc->add(workpostscommentPeer::POST_ID,$this->post->getId());  
   $postcommentsc->addAscendingOrderByColumn(workpostscommentPeer::ID);

   $this->postcomments = workpostscommentPeer::doSelect($postcommentsc);
  }
  
  public function executeNew()
  {
  
 		if ($this->getRequest()->getMethod() != sfRequest::POST)
	    {
	      // display the form
	      $this->getRequest()->setAttribute('referer', $this->getRequest()->getReferer());
	    }
	    else
	    {
  
			$newpost = new workposts();
			$newpost->setTitle(htmlentities($this->getRequestParameter('title'),ENT_QUOTES,'utf-8'));
			$newpost->setDescription(htmlentities($this->getRequestParameter('description'),ENT_QUOTES,'utf-8'));
			$newpost->setPostType($this->getRequestParameter('type'));
			$newpost->setPrivatePost($this->getRequestParameter('privacy'));
			$newpost->setAuthorId($this->getUser()->getSignedUserId());
			$newpost->setPosition($this->getRequestParameter('position'));
			$newpost->setMincommitment($this->getRequestParameter('mincomit'));
			$newpost->setBasicsalary($this->getRequestParameter('salary'));
			$newpost->setEquity($this->getRequestParameter('equity'));
			$newpost->setFounded($this->getRequestParameter('founded'));
			$newpost->setFounders($this->getRequestParameter('founders'));
			$newpost->setStage($this->getRequestParameter('stage'));
			$newpost->setUrl($this->getRequestParameter('url'));
			$newpost->setWorkFrom($this->getRequestParameter('workfrom'));
			$newpost->save();
			

			
			
			  //Lets create some tags baby!
		
		  if ($this->getRequestParameter('tags') == '') {
			//Lets do nothing bout the tags please
		  } else {
		  $TagsCut = array();
		  
		  if (strstr($this->getRequestParameter('tags'), ',')) {
		
		  $TagsString = $this->getRequestParameter('tags');
			$TagsCut = explode(",", $TagsString);
		
	  	  } else {
		
		  $TagsCut[] = $this->getRequestParameter('tags');
		
		  }
			$twitterrag = '';

			foreach ($TagsCut as $TagCut) {
				
				$pat[0] = "/\s+/";
			
				$rep[0] = "";
			
				$str = preg_replace($pat,$rep,$TagCut);
				
				if ($str != '') {
				$tag = workpoststags::getTag($str);
				$newPost2Tag = new workpoststags2post();
				$newPost2Tag->setTagId($tag->getId());
				$newPost2Tag->setPostId($newpost->getId());
				$newPost2Tag->save();
				}
				
			  
			}
		  }
			
			$username = "workforoptions";
			$password = "meta11ica02"; 
			// initialize the twitter class
			$twitter = new Twitter($username, $password);
		
			// fetch your profile in xml format
		
			$tinyurl = tinyurl::maketinyurl('http://www.workforoptions.com/post/show/?id='.$newpost->getId().'');
			$twitterstatus = substr($newpost->getTitle(), 0, 120);		
			$twitter->updateStatus('Job: '.$twitterstatus.'... #'.$newpost->getPostType().' #'.$newpost->getEquity().' '.$tinyurl.'');		  

			
			return $this->redirect('/post/show/?id='.$newpost->getId());

		
		
			  
		}	  
  
  }
  
  public function executeCommentAjax() {
  
  
  		$commentData = json_decode($this->getRequestParameter('commentObj'), true);

		$newComment = new workpostscomment();
		$newComment->setBody(htmlentities($commentData['body'],ENT_QUOTES,'utf-8'));
		$newComment->setAuthorId($this->getUser()->getSignedUserId());
		$newComment->setPostId($commentData['id']);
		$newComment->setPrivateComment($commentData['privacy']);
		$newComment->save();

		$JsonMsg = array('id' => $newComment->getId(), 'Body' => $newComment->getBody(), 'AuthorId' => $newComment->getAuthorId(), 'AuthorName' => $this->getUser()->getFullName(), 'Date' => $newComment->getCreatedAt(), 'Privacy' => $newComment->getPrivateComment());

		 //Mail the post job author about new comment
		$post = workpostsPeer::retrieveByPK($newComment->getPostId());
		$postAuthor = UserPeer::retrieveByPK($post->getAuthorId());	  
$body ='Hello '.$postAuthor->getFullName().',<br>
Someone just commented on your job listing on workforoptions.com:<br>
<a href="http://www.workforoptions.com/post/show/?id='.$post->getId().'">Click here to view it.</a><br><br>The Workforoptions team.<br><img src="http://www.workforoptions.com/images/logo.gif">';
			  
		sfNotifications::mailAnonUser($postAuthor->getEmail(),'New comment on workforoptions.com!',$body);



		 return $this->returnJSON($JsonMsg);
  
  
  }
  
    public function handleErrorNew()
  {
    return sfView::SUCCESS;
  }
  
  
}
